package com.qf.cqut.controller;

import com.qf.cqut.Service.UserService;
import com.qf.cqut.model.pojo.AUser;
import com.qf.cqut.result.ResponseCode;
import com.qf.cqut.result.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("users")
@CrossOrigin("*")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("login")
    @ResponseBody
    public ResponseData<AUser> login(String username,String password){

        AUser aUser = userService.login(username,password);
        if(aUser == null){
            return new ResponseData<>(ResponseCode.USER_INFO_ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS,aUser);
        }
    }

    @RequestMapping("list")
    @ResponseBody
    public ResponseData<List<AUser>> queryAll(Integer page, Integer limit){

        List<AUser> aUserList = userService.selectByLimit(page,limit);
        Integer count = userService.count();
        if(aUserList.isEmpty()){
            return new ResponseData<>(ResponseCode.ERROR);
        }
        return new ResponseData<>(ResponseCode.SUCCESS,count,aUserList);
    }
    @RequestMapping("del")
    @ResponseBody
    public ResponseData delById(String[] ids){
        Integer i = userService.delByIds(ids);
        if(i == 0) {
            return new ResponseData(ResponseCode.ERROR);
        }else{
            return new ResponseData(ResponseCode.SUCCESS);
        }
    }

    @RequestMapping("conditions")
    @ResponseBody
    public ResponseData<List<AUser>> queryByConditions(AUser aUser){
        List<AUser> aUserList = userService.queryByConditions(aUser);
        return new ResponseData<>(ResponseCode.SUCCESS,aUserList);
    }

    @RequestMapping("conditions1")
    @ResponseBody
    public ResponseData<List<AUser>> queryByConditions1(AUser aUser){
        List<AUser> aUserList = userService.queryByConditions1(aUser);
        return new ResponseData<>(ResponseCode.SUCCESS,aUserList);
    }
    @RequestMapping("add")
    @ResponseBody
    public ResponseData addAuser(AUser aUser){
        aUser.setCreateTime(LocalDateTime.now());
        Integer i = userService.addAuser(aUser);

        if(i == 0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }

    @RequestMapping("edit")
    @ResponseBody
    public ResponseData editAuser(AUser aUser){
        aUser.setUpdateTime(LocalDateTime.now());
        Integer i = userService.editAuser(aUser);
        if(i == 0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }

    @RequestMapping("edit1")
    @ResponseBody
    public ResponseData editAuser1(AUser aUser){
        aUser.setUpdateTime(LocalDateTime.now());
        Integer i = userService.editAuser1(aUser);
        if(i == 0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }
}
